home *** CD-ROM | disk | FTP | other *** search
/ Hacker's Secrets 4 / Hacker's Secrets 4.iso / internet / bug14.ide < prev    next >
Text File  |  1997-01-19  |  4KB  |  140 lines

  1.  
  2. /*
  3.  *   ident-scan [v0.15]
  4.  *   This TCP scanner has the additional functionality of retrieving
  5.  *   the username that owns the daemon running on the specified port.
  6.  *   It does this by by attempting to connect to a TCP port, and if it
  7.  *   succeeds, it will send out an ident request to identd on the
  8.  *   remote host.  I believe this to be a flaw in the design of the
  9.  *   protocol, and if it is the developers intent to allow 'reverse'
  10.  *   idents, then it should have been stated clearer in the
  11.  *   rfc(rfc1413).
  12.  *
  13.  *   USES:
  14.  *   It can be useful to determine who is running daemons on high ports
  15.  *   that can be security risks.  It can also be used to search for
  16.  *   misconfigurations such as httpd running as root, other daemons
  17.  *   running under the wrong uids.
  18.  *
  19.  *   COMPILES:  Compiles fine under Linux, BSDI and SunOS 4.1.x.
  20.  *
  21.  *   Dave Goldsmith
  22.  *   <daveg@escape.com>
  23.  *   02/11/1996
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <netinet/in.h>
  33. #include <netdb.h>
  34. #include <unistd.h>
  35.  
  36. enum errlist
  37. {
  38.   BAD_ARGS,BAD_HOST,NO_IDENT,SOCK_ERR
  39. };
  40.  
  41. void
  42. usage(error)
  43. enum errlist error;
  44. {
  45.   fprintf(stderr,"ident-scan: ");
  46.   switch(error)
  47.   {
  48.     case BAD_ARGS: fprintf(stderr,"usage: ident-scan hostname [low port] [hi port]\n");
  49.                    break;
  50.     case BAD_HOST: fprintf(stderr,"error: cant resolve hostname\n");
  51.                    break;
  52.     case NO_IDENT: fprintf(stderr,"error: ident isnt running on host\n");
  53.                    break;
  54.     case SOCK_ERR: fprintf(stderr,"error: socket() failed\n");
  55.                    break;
  56.   }
  57.   exit(-1);
  58. }
  59.  
  60. struct hostent *
  61. fill_host(machine,host)
  62. char *machine;
  63. struct hostent *host;
  64. {
  65.  
  66.   if ((host=gethostbyname(machine))==NULL)
  67.   {
  68.      if ((host=gethostbyaddr(machine,4,AF_INET))==NULL)
  69.         return(host);
  70.   }
  71.   return(host);
  72. }
  73.  
  74. int
  75. main(argc,argv)
  76. int argc;
  77. char **argv;
  78. {
  79.   struct sockaddr_in forconnect,forport,forident;
  80.   int i,sockfd,identfd,len=sizeof(forport),hiport=9999,loport=1,curport;
  81.   struct servent *service;
  82.   struct hostent *host;
  83.   char identbuf[15], recieved[85], *uid;
  84.  
  85.   if ((argc<2) || (argc>4))
  86.     usage(BAD_ARGS);
  87.   if (argc>2)
  88.      loport=atoi(argv[2]);
  89.   if (argc>3)
  90.      hiport=atoi(argv[3]);
  91.   if ((host=fill_host(argv[1],host))==NULL)
  92.     usage(BAD_HOST);
  93.   forconnect.sin_family=host->h_addrtype;
  94.   forconnect.sin_addr.s_addr=*((long *)host->h_addr);
  95.   forident.sin_family=host->h_addrtype;
  96.   forident.sin_addr.s_addr=*((long *)host->h_addr);
  97.   forident.sin_port=htons(113);
  98.  
  99.   if ((identfd=socket(AF_INET,SOCK_STREAM,0))== -1)
  100.      usage(SOCK_ERR);
  101.   if ((connect(identfd,(struct sockaddr *)&forident,sizeof(forident)))!=0)
  102.      usage(NO_IDENT);
  103.   close(identfd);
  104.  
  105.   for(curport=loport;curport<=hiport;curport++)
  106.   {
  107.      for(i=0;i!=85;i++)
  108.         recieved[i]='\0';
  109.      forconnect.sin_port=htons(curport);
  110.      if ((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1)
  111.         usage(SOCK_ERR);
  112.  
  113.  
  114.      if (connect(sockfd,(struct sockaddr *)&forconnect,sizeof(forconnect))==0)
  115.      {
  116.        if (getsockname(sockfd,(struct sockaddr *)&forport,&len)==0)
  117.        {
  118.           if ((identfd=socket(AF_INET,SOCK_STREAM,0))== -1)
  119.              usage(SOCK_ERR);
  120.           if (connect(identfd,(struct sockaddr *)&forident,sizeof(forident))==0)
  121.           {
  122.              sprintf(identbuf,"%u,%u",htons(forconnect.sin_port),
  123.                 htons(forport.sin_port));
  124.  
  125.              write(identfd,identbuf,strlen(identbuf)+1);
  126.              read(identfd,recieved,80);
  127.              recieved[strlen(recieved)-1]='\0';
  128.              uid=strrchr(recieved,' ');
  129.              service=getservbyport(forconnect.sin_port,"tcp");
  130.              printf("Port: %3d\tService: %10s\tUserid: %s\n",curport,
  131.                 (service==NULL)?"(?)":service->s_name,uid);
  132.           }
  133.        }
  134.     }
  135.     close(sockfd);
  136.     close(identfd);
  137.   }
  138. }
  139.  
  140.